package com.jsj.bs.controller;

import com.github.pagehelper.PageInfo;
import com.jsj.bs.pojo.Result;
import com.jsj.bs.pojo.TbGaipai;
import com.jsj.bs.pojo.TbLeader;
import com.jsj.bs.pojo.TbOrder;
import com.jsj.bs.service.TbGaipaiService;
import com.jsj.bs.service.TbLeaderService;
import com.jsj.bs.service.TbOrderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * (TbGaipai)表控制层
 *
 * @author makejava
 * @since 2020-01-09 16:30:54
 */
@Controller
@RequestMapping("/tbGaipai")
public class TbGaipaiController {

    private static final Logger LOGGER = LogManager.getLogger(TbGaipaiController.class);
    /**
     * 服务对象
     */
    @Autowired
    private TbGaipaiService tbGaipaiService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private TbLeaderService tbLeaderService;

    //返回结果集
    private Result resultMsg = new Result();

    /**
     * 添加改派信息
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/addGaiPai", method = RequestMethod.POST)
    @ResponseBody
    public Result addGaiPai(@RequestParam("gaipaiTitle") String gaipaiTitle,
                              @RequestParam("gaipaiWhy") String gaipaiWhy,
                              @RequestParam("orderId") int orderId,
                              @RequestParam("leaderId") int leaderId,
                              Model model) {

        LOGGER.info("====TbGaipaiController======" + gaipaiTitle + "=====" + gaipaiWhy + "========" + orderId);

        TbGaipai tbGaipai = new TbGaipai();

        tbGaipai.setGaipaiTitle(gaipaiTitle);
        tbGaipai.setGaipaiWhy(gaipaiWhy);
        tbGaipai.setOdertId(orderId);
        tbGaipai.setLeaderId(leaderId);
        tbGaipai.setGaipaiDate(new Date());
        LOGGER.info("==========" + tbGaipai);

        TbGaipai toTbGaipai = tbGaipaiService.insert(tbGaipai);

        if (toTbGaipai != null) {
            resultMsg.setFlag(true);
            resultMsg.setMsg("操作成功");
        }else {
            resultMsg.setFlag(false);
            resultMsg.setMsg("操作失败");
        }
        return resultMsg;
    }

    /**
     * 根据负责人查询此人改派申请
     *
     * @param page     页码
     * @param pageSize 页大小
     * @param leaderId 负责人
     * @return PageInfo<TbGaipai> 分页数据集合
     */
    @RequestMapping(value = "searchByLeaderId", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<TbGaipai> searchByLeaderId(@RequestParam(value = "page", defaultValue = "1") int page,
                                               @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                                               @RequestParam("leaderId") int leaderId) {

        LOGGER.info(leaderId + "==================负责人id============");
        TbGaipai tbGaipai = new TbGaipai();
        tbGaipai.setLeaderId(leaderId);
        return tbGaipaiService.queryAll(page, pageSize, tbGaipai);
    }

    /**
     * 删除改派信息
     * @param tbGaipai
     * @return
     */
    @RequestMapping(value = "delGaiPai", method = RequestMethod.POST)
    @ResponseBody
    public Boolean delGaiPai(TbGaipai tbGaipai) {

        LOGGER.info("=================del del del ==========="+tbGaipai.getGaipaiId());

        boolean deleteById = tbGaipaiService.deleteById(tbGaipai.getGaipaiId());
        if (deleteById){
            return true;
        }
        return false;
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    @ResponseBody
    public TbGaipai selectOne(Integer id) {
        return tbGaipaiService.queryById(id);
    }


    /**
     * 查询多条数据
     *
     * @param page     页码
     * @param pageSize 页大小
     * @param tbGaipai 实体
     * @return PageInfo<TbGaipai> 分页数据集合
     */
    @RequestMapping(value = "searchAll", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<TbGaipai> searchAll(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize, TbGaipai tbGaipai) {
        return tbGaipaiService.queryAll(page, pageSize, tbGaipai);
    }

    /**
     * 后台展示数据
     * @param page
     * @param pageSize
     * @param tbGaipai
     * @param model
     * @return
     */
    @RequestMapping(value = "searchGaipais", method = RequestMethod.GET)
    public String searchGaipais(@RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
                               TbGaipai tbGaipai, Model model) {
        LOGGER.info(tbGaipai + "=========" + page + "====" + pageSize + "=========" + tbGaipai);
        PageInfo<TbGaipai> tbOrderPageInfos = tbGaipaiService.queryAll(page, pageSize, tbGaipai);
        LOGGER.info("======后台展示数据==========" + tbOrderPageInfos);
        model.addAttribute("tbOrderPageInfos", tbOrderPageInfos);
        return "gaipai-manager";
    }

    /**
     * 后台查询单条数据
     *
     * @param gaipaiId 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/selectTbGaipai", method = RequestMethod.GET)
    public String selectTbOrder(int gaipaiId,int type,Model model) {
        LOGGER.info("=============后台查询单条数据=====================" + gaipaiId);
        TbGaipai tbGaipai = tbGaipaiService.queryById(gaipaiId);
        LOGGER.info("=====111====tbGaipai========"+tbGaipai);
        model.addAttribute("tbOrder",tbGaipai);
        // type=3  表示被驳回  驳回页需要 申请人名字  也就是负责人名字  所以需要tbLeader
        //type=4  表示同意 需要重新指定人 指派页需要所有维修负责人的姓名 所以需要tbLeaders
        TbOrder tbOrder = tbOrderService.queryById(tbGaipai.getOdertId());
        if (type ==3){
            TbLeader tbLeader = tbLeaderService.queryById(tbGaipai.getLeaderId());
            LOGGER.info("======2222===tbLeader========"+tbLeader);

            model.addAttribute("tbGaipai",tbGaipai);
            model.addAttribute("tbLeader", tbLeader);
            model.addAttribute("tbOrder", tbOrder);
            return "bohui-page";
        }else{
            tbGaipai.setTbOrder(tbOrderService.queryById(tbGaipai.getOdertId()));
            model.addAttribute("tbGaipai",tbGaipai);
            LOGGER.info("====3333=====tbGaipai========"+tbGaipai);
            List<TbLeader> tbLeaders = tbLeaderService.searchLeaders();

            for (TbLeader tbLeader : tbLeaders) {
                LOGGER.info("====4444=====tbLeader========"+tbLeader);
            }
            model.addAttribute("tbLeaders", tbLeaders);
            model.addAttribute("tbOrder", tbOrder);
            return "pizhun-page";
        }

    }

    /**
     * 后台 改派单批准  改派新的维修负责人
     *
     * @param gaipaiId
     * @param leaderId
     * @return
     */
    @RequestMapping(value = "/pizhunNew", method = RequestMethod.POST)
    public String pizhunNew(Integer gaipaiId,Integer leaderId,Model model) {
       TbGaipai tbGaipai = new TbGaipai();
       tbGaipai.setGaipaiId(gaipaiId);
       tbGaipai.setGaipaiStatu("批准");
        Boolean aBoolean = tbGaipaiService.updateHt(tbGaipai);
        LOGGER.info("+=====后台 根据id更新改派单状态和状态详情========pizhunNew===========+"+tbGaipai+"==============" +aBoolean);
        //根据改派id查询改派单信息
        TbGaipai tbGaipai1 = tbGaipaiService.queryById(gaipaiId);
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderId(tbGaipai1.getOdertId());
        tbOrder.setStatu("未维修");
        tbOrder.setLeaderId(leaderId);
        Boolean aBoolean1 = tbOrderService.update(tbOrder);
        LOGGER.info("+=====后台 根据id更新报修单状态和状态详情========pizhunNew===========+"+tbGaipai+"==============" +aBoolean1);
        if (aBoolean && aBoolean1){
            model.addAttribute("StatusMsg", 3);
            return "updateresult";
        }else {
            model.addAttribute("StatusMsg", 4);
            return "updateresult";
        }
    }

    /**
     * 后台 改派单批准  驳回
     *
     * @param gaipaiId
     * @return
     */
    @RequestMapping(value = "/bohuiHt", method = RequestMethod.POST)
    public String bohuiHt(Integer gaipaiId,String gaipaiWhy,Model model) {
        TbGaipai tbGaipai = new TbGaipai();
        tbGaipai.setGaipaiId(gaipaiId);
        tbGaipai.setGaipaiStatu("驳回");
        tbGaipai.setGaipaiWhy(gaipaiWhy);
        Boolean aBoolean = tbGaipaiService.updateHt(tbGaipai);
        LOGGER.info("+=====后台 根据id更新报修单状态和状态详情========bohuiHt===========+"+tbGaipai+"==============" +aBoolean);
        if (aBoolean){
            model.addAttribute("StatusMsg", 3);
            return "updateresult";
        }else {
            model.addAttribute("StatusMsg", 4);
            return "updateresult";
        }
    }
    /**
     * 后台 根据id删除改派单
     *
     * @param gaipaiId
     * @return
     */
    @RequestMapping(value = "/DeleteGaiPai", method = RequestMethod.GET)
    @ResponseBody
    public Result DeleteGaiPai(Integer gaipaiId) {
        LOGGER.info( "+=====后台 根据id删除DeleteGaiPai==========+" + gaipaiId);
        Boolean result =tbGaipaiService.deleteById(gaipaiId);
        if (result) {
            LOGGER.info("======操作成功=========");
            resultMsg.setFlag(true);
            resultMsg.setMsg("操作成功");
        } else {
            LOGGER.info("======操作失败=========");
            resultMsg.setFlag(false);
            resultMsg.setMsg("操作失败");
        }
        return resultMsg;
    }
}